
package com.oahcfly.chgame.core.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.EventListener;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.AlphaAction;
import com.badlogic.gdx.scenes.scene2d.actions.SequenceAction;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.Array;
import com.oahcfly.chgame.core.Chao;
import com.oahcfly.chgame.core.listener.CHClickListener;
import com.oahcfly.chgame.core.listener.CHClickListener.CLICKTYPE;
import com.oahcfly.chgame.core.mvc.CHActor;

/**
 * 
 * 收缩条：支持4个方向收缩，展示隐藏动画的为淡入淡出
 * @author haocao
 *
 */
public class CHStretchBar extends Group {
	public enum DIRECTION {
		UP, DOWN, LEFT, RIGHT
	}

	private Array<CHActor> elementArray;
	private DIRECTION direction = DIRECTION.RIGHT;
	private float padding = 10;
	// 是否收缩
	private boolean isStretched = false;
	// 动画时长
	private float alphaDuration = 0.2f;

	/**
	 * 
	 * @param direction 弹出方向
	 * @param defaultPng 按钮默认图片
	 * @param pngArray 收缩条目图片
	 */
	public CHStretchBar (DIRECTION direction, String defaultPng, Array<String> pngArray) {
		this.direction = direction;
		elementArray = new Array<CHActor>();
		for (String path : pngArray) {
			CHActor tmpActor = CHActor.obtain();
			tmpActor.setBgTexture(Chao.game.getTexture(path));
			elementArray.add(tmpActor);
		}

		CHActor actor = CHActor.obtain();
		actor.setBgTexture(Chao.game.getTexture(defaultPng));
		addActor(actor);
		setSize(actor.getWidth(), actor.getHeight());
	}

	public CHStretchBar (DIRECTION direction, Texture defaultTexture, Array<Texture> textureArr) {
		this.direction = direction;
		elementArray = new Array<CHActor>();
		for (Texture texture : textureArr) {
			CHActor tmpActor = CHActor.obtain();
			tmpActor.setBgTexture(texture);
			elementArray.add(tmpActor);
		}
		CHActor actor = CHActor.obtain();
		actor.setBgTexture(defaultTexture);
		addActor(actor);
		setSize(actor.getWidth(), actor.getHeight());
	}

	public void show () {
		if (itemClickListener == null) {
			itemClickListener = new CHClickListener(this.stretchBarListener != null ? this.stretchBarListener.getClickType()
				: CLICKTYPE.NORMAL) {

				@Override
				public void doClicked (InputEvent event, float x, float y) {
					if (stretchBarListener != null) {
						stretchBarListener.onItemClick(getListenerActor(), (Integer)getListenerActor().getUserObject());
					}
				}

			};
		}

		if (getActions().size > 0) return;
		if (isStretched) {
			dismiss();
			return;
		}
		// 元素坐标以Group左下角(0,0)为基准
		float cx = 0, cy = 0, unitx = 0, unity = 0;
		float actorW = elementArray.get(0).getWidth();
		float actorH = elementArray.get(0).getHeight();
		float paddingx = 0, paddingy = 0;
		if (direction == DIRECTION.RIGHT) {
			// 右
			cx = getWidth() + actorW / 2 + 10;
			cy = getHeight()/2;
			unitx = actorW;
			paddingx = getPadding();
		} else if (direction == DIRECTION.LEFT) {
			// 左
			cx = 0 - actorW / 2 - 10;
			cy = getHeight()/2;
			unitx = -actorW;
			paddingx = -getPadding();
		} else if (direction == DIRECTION.UP) {
			// 上
			cx = getWidth()/2;
			cy = getHeight() + actorH / 2 + 10;
			unity = actorH;
			paddingy = getPadding();
		} else if (direction == DIRECTION.DOWN) {
			// 下
			cx = getWidth()/2;
			cy = 0 - actorH / 2 - 10;
			unity = -actorH;
			paddingy = -getPadding();
		}

		Color color = new Color(getColor());
		color.a = 0;
		int i = 0;
		for (CHActor tmpActor : elementArray) {
			if (tmpActor.getStage() != null) break;
			tmpActor.addListener(itemClickListener);
			addActor(tmpActor);
			tmpActor.setUserObject(i);
			tmpActor.setColor(color);
			tmpActor.setPosition(cx + i * paddingx + i * unitx,cy + i * paddingy + i * unity, Align.center);
			i++;
		}

		SequenceAction sequenceAction = new SequenceAction();
		int size = elementArray.size;
		for (int p = 0; p < size; p++) {
			CHActor tmpActor = elementArray.get(p);
			tmpActor.setTouchable(Touchable.enabled);
			AlphaAction apAction = Actions.alpha(1, getAlphaDuration());
			apAction.setActor(tmpActor);
			sequenceAction.addAction(apAction);
		}

		addAction(sequenceAction);

		isStretched = true;
	}

	public void dismiss () {
		isStretched = false;
		SequenceAction sequenceAction = new SequenceAction();
		int size = elementArray.size;
		for (int p = size - 1; p >= 0; p--) {
			CHActor tmpActor = elementArray.get(p);
			AlphaAction apAction = Actions.alpha(0, getAlphaDuration());
			apAction.setActor(tmpActor);
			tmpActor.setTouchable(Touchable.disabled);
			sequenceAction.addAction(apAction);
		}

		addAction(sequenceAction);
	}
	
	

	@Override
	public void act (float delta) {
		// TODO Auto-generated method stub
		super.act(delta);
	}

	public float getPadding () {
		return padding;
	}

	/**
	 * 两个item之间的间隙
	 * @param padding
	 */
	public void setPadding (float padding) {
		this.padding = padding;
	}

	public float getAlphaDuration () {
		return alphaDuration;
	}

	/**
	 * alpha动画时长
	 * @param alphaDuration
	 */
	public void setAlphaDuration (float alphaDuration) {
		this.alphaDuration = alphaDuration;
	}

	private CHClickListener itemClickListener;
	private StretchBarListener stretchBarListener;

	public void setStretchBarListener (StretchBarListener stretchBarListener) {
		this.stretchBarListener = stretchBarListener;
	}

	@Override
	public boolean addListener (EventListener listener) {
		return getChildren().get(0).addListener(listener);
	}

	public interface StretchBarListener {
		public void onItemClick (Actor actor, int position);

		public CLICKTYPE getClickType ();
	}
}
